package safeCommitter.svnIntegration;

import java.lang.reflect.Field;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.swt.widgets.Shell;
import org.tigris.subversion.subclipse.ui.comments.CommitCommentArea;
import org.tigris.subversion.subclipse.ui.wizards.dialogs.SvnWizardCommitPage;
import org.tigris.subversion.subclipse.ui.wizards.dialogs.SvnWizardDialog;

/**
 * A commit dialog which avoids blank comments commits if the user decides so.
 * 
 * 
 * @author Luiz Henrique
 * 
 */
public class SCSvnWizardDialog extends SvnWizardDialog {

	private Class<?> c;

	public SCSvnWizardDialog(final Shell parentShell, final IWizard newWizard) {
		super(parentShell, newWizard);
		try {
			c = Class
					.forName("org.tigris.subversion.subclipse.ui.wizards.dialogs.SvnWizardCommitPage");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setButtonEnablement(final Boolean buttonStatus) {
		getButton(IDialogConstants.FINISH_ID).setEnabled(buttonStatus);
	}

	public void addModifyListener() throws Exception {
		Field declaredField = c.getDeclaredField("commitCommentArea");
		declaredField.setAccessible(true);
		SvnWizardCommitPage ob = (SvnWizardCommitPage) this.getCurrentPage();
		CommitCommentArea a = (CommitCommentArea) declaredField.get(ob);
		a.addPropertyChangeListener(new SCModifyListener());

	}

	private class SCModifyListener implements IPropertyChangeListener {

		public void propertyChange(
				final org.eclipse.jface.util.PropertyChangeEvent event) {
			try {
				Boolean value = ((String) event.getNewValue()).length() > 0 ? true
						: false;
				setButtonEnablement(value);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
